package com.sky.controller.User;

import com.sky.context.BaseContext;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.RessBookService;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "c-地址管理")
public class AddressBookController {
    @Autowired
     RessBookService ressBookService;

    @PostMapping
    @ApiOperation("地址新增")
    public Result Ordersub(@RequestBody AddressBook addressBook){
        ressBookService.orderinter(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询地址")
    public Result<List<AddressBook>> list(){
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list =  ressBookService.list(addressBook);
       return Result.success(list);
    }
    /*
     * 根据id查询
     *
     * */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<AddressBook> listBid(@PathVariable long id){
        AddressBook addressBook = ressBookService.liseBid(id);
        return  Result.success(addressBook);
    }
    /*
    * 修改地址
    * */
    @PutMapping
    @ApiOperation("修改地址")
    public Result updatebook(@RequestBody AddressBook addressBook){
            ressBookService.update(addressBook);
            return Result.success();
    }
   @DeleteMapping
    @ApiOperation("根据id删除")
    public Result delteBid(long  id){
        ressBookService.deteleBId(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("更该默认值")
    public Result defaulta(@RequestBody AddressBook addressBook){
            ressBookService.updatedeflt(addressBook);
            return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> listdefult(){
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        List<AddressBook> list = ressBookService.list(addressBook);
        if (list !=null && list.size()>0){
            return Result.success(list.get(0));
        }
        return Result.error("没有默认地址");
    }
}
